<template>
  <div>
    <el-row>
      <el-col style="margin-right: 10px" :span="24" class="content-left">
        <el-row class="handleBox">
          <el-col :span="18">
            <template>
              <el-button type="normal" @click="addFunc">新增</el-button>
              <el-button type="normal" @click="editBasic">编辑</el-button>
              <el-button type="normal" @click="deleteFunc">删除</el-button>
            </template>
          </el-col>
          <el-col :span="6">
            <div class="handleIconBox">
              <el-popover
                placement="bottom"
                trigger="click"
                popper-class="icon_porper"
              >
                <div class="export_porper">
                  <!-- 导出操作区域 -->
                  <el-button @click="exportExcel">导出</el-button>
                </div>
                <i
                  class="iconfont icon-export"
                  title="导出"
                  slot="reference"
                ></i>
              </el-popover>
              <!-- 导入 -->
              <el-popover
                placement="bottom"
                trigger="click"
                popper-class="icon_porper"
              >
                <div class="import_porper">
                  <!-- 导入操作区域 -->

                  <el-upload
                    accept=".xls, .xlsx"
                    ref="upload"
                    :multiple="false"
                    :before-upload="beforeUpload"
                    :show-file-list="false"
                    action
                    :limit="1"
                    :http-request="uploadFile"
                  >
                    <el-button>导入</el-button>
                  </el-upload>
                </div>
                <i
                  class="iconfont icon-import"
                  title="导入"
                  slot="reference"
                ></i>
              </el-popover>
              <el-popover
                placement="bottom"
                trigger="click"
                popper-class="icon_porper"
              >
                <div class="search_porper">
                  <el-form
                    :model="searchForm"
                    ref="searchForm"
                    label-width="90px"
                    label-position="left"
                    size="mini"
                  >
                    <el-form-item label="指标编号：">
                      <el-input
                        v-model.trim="searchForm.realName"
                        style="width: 150px"
                      ></el-input>
                    </el-form-item>
                    <el-form-item label="生效日期">
                      <el-date-picker
                        v-model="searchForm.effectDt"
                        type="date"
                        style="width: 150px"
                        placeholder="选择日期"
                        :clearable="false"
                      ></el-date-picker>
                    </el-form-item>

                    <el-form-item label="指标名称">
                      <el-input
                        style="width: 150px"
                        placeholder="请输入"
                        v-model="searchForm.cnName"
                      ></el-input>
                    </el-form-item>

                    <el-form-item label="加工方式">
                      <el-select
                        style="width: 150px"
                        v-model="searchForm.statMethodCd"
                        placeholder="请选择"
                      >
                        <el-option
                          v-for="item in statMethodCdList"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                    <el-form-item label="指标属性之公共特性">
                      <el-select
                        style="width: 150px"
                        v-model="searchForm.attr3"
                        placeholder="请选择"
                      >
                        <el-option
                          v-for="item in attr3Option"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                    <el-form-item label="应用场景">
                      <el-select
                        style="width: 150px"
                        v-model="searchForm.useCase"
                        placeholder="请选择"
                      >
                        <el-option
                          v-for="item in useCaseList"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                    <el-form-item label="指标类型">
                      <el-select
                        style="width: 150px"
                        v-model="searchForm.zblx"
                        placeholder="请选择"
                      >
                        <el-option
                          v-for="item in zblxList"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-form>
                  <div style="text-align: center; padding: 10px">
                    <el-button
                      size="mini"
                      @click="(searchForm.page = 1), searchFunc()"
                      >查询</el-button
                    >
                    <el-button size="mini" @click="searchClear">重置</el-button>
                  </div>
                </div>
                <i
                  class="iconfont icon-sousuo"
                  title="搜索"
                  slot="reference"
                ></i>
              </el-popover>
              <el-popover
                placement="bottom"
                width="150"
                trigger="click"
                popper-class="icon_porper"
              >
                <div class="operation_checkbox">
                  <el-checkbox-group v-model="checkList">
                    <el-checkbox
                      v-for="item in textData"
                      :label="item.label"
                      :key="item.prop"
                      >{{ item.label }}
                    </el-checkbox>
                  </el-checkbox-group>
                </div>
                <i
                  class="iconfont icon-liebiaoshezhi"
                  title="列表设置"
                  slot="reference"
                ></i>
              </el-popover>
            </div>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
    <el-table
      ref="multipleTable"
      :data="tableData"
      @row-click="rowClick"
      style="width: 100%"
    >
      <el-table-column
        width="55"
        type="selection"
        
      ></el-table-column>
      <el-table-column
        v-for="(item, key) in showData"
        align="center"
        :key="key"
        :prop="item.prop"
        :label="item.label"
        :show-overflow-tooltip="true"
        :render-header="item.render"
        :sortable="item.sortable"
        :formatter="item.formatter"
        :width="item.width"
        :min-width="item.minwidth"
      ></el-table-column>
    </el-table>
    <pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchForm.page"
      @refresh="searchFunc"
      :page-size="searchForm.size"
      :total="total"
    ></pagination>
    <!-- 新增弹框 -->
    <addBasic
      :addBasicVisible="addBasicVisible"
      :cancelBasicM="cancelBasicM"
      :addBasicM="addBasicM"
      :closeAddDialog="closeAddDialog"
      ref="addBasicData"
    ></addBasic>
    <!-- 新增弹框 -->
    <!-- 编辑弹框 -->
    <editBasic
      :editBasicVisible="editBasicVisible"
      :cancelEditBasicM="cancelEditBasicM"
      :editBasicM="editBasicM"
      :closeEditDialog="closeEditDialog"
      ref="editBasicData"
    ></editBasic>
    <!-- 编辑弹框 -->
    <!-- 遮罩层删除 -->
    <el-dialog
      class="mask-del"
      :visible.sync="centerDialogVisible"
      width="400px"
      center
    >
      <div class="del-content">
        <i class="icon-color el-icon-warning"></i>确认要删除吗？
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          class="mask-button-primary button-big"
          type="primary"
          @click="sure"
          >确 定</el-button
        >
        <el-button
          class="mask-button-normal button-big"
          @click="centerDialogVisible = false"
          >取 消</el-button
        >
      </div>
    </el-dialog>
    <!-- 遮罩层删除 -->
  </div>
</template>
<script>
import { DatasCenter } from "@/api/index";
import editBasic from "./editBasic.vue";
import addBasic from "./addBasic.vue";
import { dateFormat } from "@/lib/util.js";
import moment from "moment";
export default {
  name: "basicTable",
  components: {
    addBasic,
    editBasic,
  },
  data() {
    return {
      checkList: [],
      useCaseList: [], // 应用场景
      zblxList: [], // 指标类型
      statMethodCdList: [], // 加工方式
      calcUnitTypeCdList: [],
      statFreqList: [],
      excelFile: "",
      editBasicVisible: false,
      addBasicVisible: false, // 新增属性弹框
      id: "", // 表格唯一标识符
      centerDialogVisible: false, // 删除弹框显示隐藏
      shrink: true, // 收缩展开状态
      attr3Option: [], // 指标属性之共性个性
      searchForm: {
        effectDt: new Date(),
        idxNo: "",
        cnName: "",
        statMethodCd: "",
        attr3: "",
        useCase: "",
        zblx: "",
        page: 1,
        size: 15
      },
      // // 第几页
      // page: 1,
      // // 页面展示个数
      // size: 15,
      // 总数
      total: 0,
      tableData: [], // 表格数据
      textData: [
        {
          prop: "effectDt",
          label: "生效日期",
          width: 125,
          sortable: true,
          align: "center",
          checkDisabled: false,
        },
        {
          prop: "invalidDt",
          label: "失效日期",
          width: 125,
          sortable: true,
          checkDisabled: false,
        },
        {
          prop: "idxAttr",
          label: "指标维度",
          checkDisabled: false,
          width: 250,
        },
        {
          prop: "idxNo",
          label: "指标编号",
          sortable: true,
          width: 120,
          checkDisabled: false,
        },
        {
          prop: "cnName",
          label: "指标名称",
          sortable: true,
          width: 100,
          checkDisabled: false,
        },
        {
          prop: "useCase",
          label: "应用场景",
          width: 150,
          checkDisabled: false,
          formatter: (row) => {
            return this.$getLabel(row.useCase, this.useCaseList);
          },
        },

        {
          prop: "zblx",
          label: "指标类型",
          width: 100,
          checkDisabled: false,
          formatter: (row) => {
            return this.$getLabel(row.zblx, this.zblxList);
          },
        },
        {
          prop: "idxClsAttrCd",
          label: "指标分类属性",
          width: 130,
          checkDisabled: false,
        },
        {
          prop: "caliberNames",
          label: "统计口径",
          width: 130,
          checkDisabled: false,
        },
        {
          prop: "calcFormula",
          label: "计算公式",
          width: 320,
          checkDisabled: false,
        },
        {
          prop: "calcUnitTypeCd",
          label: "计量单位类别",
          width: 120,
          checkDisabled: false,
          formatter: (row) => {
            return this.$getLabel(row.calcUnitTypeCd, this.calcUnitTypeCdList);
          },
        },
        {
          prop: "statMethodCd",
          label: "加工方式",
          width: 120,
          checkDisabled: false,
          formatter: (row) => {
            return this.$getLabel(row.statMethodCd, this.statMethodCdList);
          },
        },
        {
          prop: "statFreq",
          label: "首页指标",
          width: 120,
          checkDisabled: false,
          formatter: (row) => {
            return this.$getLabel(row.statFreq, this.statFreqList);
          },
        },

        {
          prop: "memo",
          label: "备注",
          width: 120,
          checkDisabled: false,
        },
        {
          prop: "tabName",
          label: "结果表",
          width: 120,
          checkDisabled: false,
        },
        {
          prop: "modifyOperNo",
          label: "修订人",
          width: 120,
          checkDisabled: false,
          formatter: (row) => {
            if (row.modifyOperNo) {
              return row.modifyOperNo;
            } else {
              return "-";
            }
          },
        },
        {
          prop: "modifyTime",
          label: "修订日期",
          width: 120,
          checkDisabled: false,
          formatter: (row) => {
            if (row.modifyTime) {
              return moment(row.modifyTime).format("YYYY-MM-DD HH:mm:ss"); // 24小时制
            } else {
              return "-";
            }
          },
        },
      ],
    };
  },
  created() {
    this.getDefaultData();
    this.checkListInit();
  },
  computed: {
    // 展示字段
    showData() {
      let arr = this.textData.map((v) => {
        return v.label;
      });
      let valArr = [],
        showData = [];
      // 选中的字段排序
      arr.forEach((element) => {
        let flag = false;
        this.checkList.forEach((e) => {
          if (e == element) {
            flag = true;
          }
        });
        if (flag) {
          valArr.push(element);
        }
      });
      // 控制表列隐藏显示
      for (let i = 0; i < this.checkList.length; i++) {
        for (let j = 0; j < this.textData.length; j++) {
          if (this.textData[j].label == valArr[i]) {
            let item = { ...this.textData[j] };
            showData.push(item);
          }
        }
      }
      return showData;
    },
  },

  mounted() {
    this.searchFunc();
  },
  methods: {
    checkListInit() {
      this.checkList = this.textData.map((v) => {
        return v.label;
      });
    },
    searchClear() {
      for (const key in this.searchForm) {
        this.searchForm[key] = "";
      }
      this.searchForm.page = 1;
      this.searchForm.size = 15;
    },
    rowClick(row) {
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleRowSelection(row, true);
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.searchForm.size = val;
      this.searchFunc();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.searchForm.page = val;
      this.searchFunc();
    },
    // 上传文件之前的钩子
    beforeUpload(file) {
      let name = file.name;
      let type = name.substring(name.indexOf("."), name.length);
      if (type === ".xls" || type === ".xlsx") {
        return true;
      }
      this.$message({
        message: "只能选择xls或者xlsx格式的文件上传",
        type: "warning",
        showClose: true,
      });
      return false;
    },
    // 上传文件
    uploadFile(item) {
      this.excelFile = item.file;
      let form = new FormData();
      // 文件对象
      form.append("uploadfile", this.excelFile);
      var that = this;
      DatasCenter.impIndexBase(form)
        .then((response) => {
          if (response.code == 200) {
            that.$message({
              type: "success",
              message: "文件：" + this.excelFile.name + "上传成功",
            });
            that.$refs.upload.clearFiles();
            that.searchFunc();
          } else {
            that.$message({
              type: "error",
              message: response.message,
            });
            that.$refs.upload.clearFiles();
            that.searchFunc();
          }
        })
        .catch(function () {
          that.$message({
            type: "warning",
            message: "文件内容与模板不相符，导入失败",
          });
        });
    },
    //导出
    exportExcel() {
      if (this.searchForm.effectDt) {
        this.searchForm.effectDt = dateFormat(this.searchForm.effectDt);
      }
      let forms = {
        effectDt: this.searchForm.effectDt,
        idxNo: this.searchForm.idxNo,
        cnName: this.searchForm.cnName,
        statMethodCd: this.searchForm.statMethodCd,
        attr3: this.searchForm.attr3,
        useCase: this.searchForm.useCase,
        zblx: this.searchForm.zblx,
        page: 1,
        size: 999999,
      };
      DatasCenter.expIndexBase(forms)
        .then((response) => {
          if (response) {
            var filename = decodeURI(
              response.headers["content-disposition"]
            ).split("=")[1];
            let contenttype =
              filename.substring(filename.lastIndexOf("."), filename.length) ===
              "xlsx"
                ? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
                : "application/vnd.ms-excel;charset=utf-8";
            var blob = new Blob([response.data], {
              type: contenttype,
            });
            if (window.navigator.msSaveOrOpenBlob) {
              navigator.msSaveBlob(blob, filename);
            } else {
              var downloadElement = document.createElement("a");
              var href = window.URL.createObjectURL(blob); //创建下载的链接
              downloadElement.href = href;
              downloadElement.download = filename; //下载后文件名
              document.body.appendChild(downloadElement);
              downloadElement.click(); //点击下载
              document.body.removeChild(downloadElement); //下载完成移除元素
              window.URL.revokeObjectURL(href); //释放掉blob对象
            }
          }
        })
        .catch(function (error) {
          console.log(error);
          this.$message({
            type: "error",
            message: "导出失败!",
          });
        });
    },
    // 获取下拉框数据
    getDefaultData() {
      // 获取指标属性之共性个性
      console.log("获取指标属性");
      this.$getDict("idx_attr_3").then((res) => {
        if (res.code == 200) {
          this.attr3Option = res.datas;
          localStorage.setItem("attr3Option", JSON.stringify(this.attr3Option));
        }
      }),
        this.$getDict("use_case").then((res) => {
          if (res.code == 200) {
            this.useCaseList = res.datas;
            localStorage.setItem(
              "useCaseList",
              JSON.stringify(this.useCaseList)
            );
          }
        }),
        this.$getDict("zblx").then((res) => {
          if (res.code == 200) {
            this.zblxList = res.datas;
            localStorage.setItem("zblxList", JSON.stringify(this.zblxList));
          }
        }),
        this.$getDict("stat_method_cd").then((res) => {
          if (res.code == 200) {
            this.statMethodCdList = res.datas;
            localStorage.setItem(
              "statMethodCdList",
              JSON.stringify(this.statMethodCdList)
            );
          }
        }),
        this.$getDict("Calc_Unit_Type_Cd").then((res) => {
          if (res.code == 200) {
            this.calcUnitTypeCdList = res.datas;
            localStorage.setItem(
              "calcUnitTypeCdList",
              JSON.stringify(this.calcUnitTypeCdList)
            );
          }
        }),
        this.$getDict("stat_Freq").then((res) => {
          if (res.code == 200) {
            this.statFreqList = res.datas;
            localStorage.setItem(
              "statFreqList",
              JSON.stringify(this.statFreqList)
            );
          }
        });
    },
    // 条件查询
    searchFunc() {
      //   this.page = 1;
      this.getPageBasicTableList();
    },
    // 获取表格数据
    getPageBasicTableList() {
      if (this.searchForm.effectDt) {
        this.searchForm.effectDt = dateFormat(this.searchForm.effectDt);
      }
      let form = {
        sort: "modifyTime",
        order: "desc",
        page: this.searchForm.page,
        size: this.searchForm.size,
        effectDt: this.searchForm.effectDt,
        idxNo: this.searchForm.idxNo,
        cnName: this.searchForm.cnName,
        statMethodCd: this.searchForm.statMethodCd,
        attr3: this.searchForm.attr3,
        useCase: this.searchForm.useCase,
        zblx: this.searchForm.zblx,
      };
      if (
        this.useCaseList &&
        this.zblxList &&
        this.statMethodCdList &&
        this.calcUnitTypeCdList &&
        this.statFreqList
      ) {
        DatasCenter.findPageIndexBase(form).then((res) => {
          // 处理获取的数据
          this.tableData = res.data;
          this.total = res.count;
          for (var i = 0; i < this.tableData.length; i++) {
            this.tableData[i].effectDt = dateFormat(this.tableData[i].effectDt);
            if (this.tableData[i].invalidDt) {
              this.tableData[i].invalidDt = dateFormat(
                this.tableData[i].invalidDt
              );
            }
            if (this.tableData[i].calcFormula) {
              if (this.tableData[i].calcFormula.length > 40) {
                this.tableData[i].calcFormula =
                  this.tableData[i].calcFormula.slice(0, 40) + "...";
              }
            }
            if (this.tableData[i].memo) {
              if (this.tableData[i].memo.length > 40) {
                this.tableData[i].memo =
                  this.tableData[i].memo.slice(0, 40) + "...";
              }
            }
            if (this.tableData[i].modifyTime) {
              this.tableData[i].modifyTime = dateFormat(
                this.tableData[i].modifyTime
              );
            }
          }
        });
      }
    },
    // 点击新增按钮
    addFunc() {
      this.addBasicVisible = true;
      this.$refs["addBasicData"].caliberTableDataShow = false;
      this.$refs["addBasicData"].caliberTableIconShow = [];
      this.$refs["addBasicData"].addForm.increaseIdxAttr = [];
      (this.$refs["addBasicData"].addForm.cnName = ""),
        (this.$refs["addBasicData"].addForm.idxNo = ""),
        (this.$refs["addBasicData"].addForm.increaseUseCase = ""),
        (this.$refs["addBasicData"].addForm.increaseZblx = ""),
        (this.$refs["addBasicData"].addForm.increaseCalcUnitTypeCd = ""),
        (this.$refs["addBasicData"].addForm.increaseStatMethodCd = ""),
        (this.$refs["addBasicData"].addForm.increaseEffectDt = "");
      this.$refs["addBasicData"].addForm.increaseInvalidDt = "";
      this.$refs["addBasicData"].addForm.increaseMemo = "";
      (this.$refs["addBasicData"].addForm.increaseCalFormula = ""),
        (this.$refs["addBasicData"].addForm.increaseAttr3 = ""),
        (this.$refs["addBasicData"].addForm.addCaliberInput = []),
        (this.$refs["addBasicData"].addForm.increaseStatFreq = "");
      this.$refs["addBasicData"].findCaliberAttr();
      this.$refs["addBasicData"].getIncreaseIdxAttrs();
      this.$nextTick(() => {
        this.$refs["addBasicData"].$refs.addForm.clearValidate();
      });
    },
    // 新增弹框确定按钮
    addBasicM() {
      if (this.$refs["addBasicData"].addForm.increaseInvalidDt) {
        this.$refs["addBasicData"].addForm.increaseInvalidDt = dateFormat(
          this.$refs["addBasicData"].addForm.increaseInvalidDt
        );
      }
      this.$refs["addBasicData"].$refs.addForm.validate((valid) => {
        if (valid) {
          var param = `cnName=${this.$refs["addBasicData"].addForm.cnName}
          &idxNo=${this.$refs["addBasicData"].addForm.idxNo}
          &useCase=${this.$refs["addBasicData"].addForm.increaseUseCase}
          &zblx=${this.$refs["addBasicData"].addForm.increaseZblx}
          &attr3=${this.$refs["addBasicData"].addForm.increaseAttr3}
          &calcUnitTypeCd=${this.$refs["addBasicData"].addForm.increaseCalcUnitTypeCd}
          &statMethodCd=${this.$refs["addBasicData"].addForm.increaseStatMethodCd}
          &statFreq=${this.$refs["addBasicData"].addForm.increaseStatFreq}`;
          this.$refs["addBasicData"].addForm.increaseIdxAttr.forEach(
            (value) => (param += `&idxAttr=${value}`)
          );
          this.$refs["addBasicData"].addForm.addCaliberInput.forEach(
            (value, index) =>
              (param += `&caliberNo${index + 1}=${value.caliberNo}`)
          );
          this.$refs["addBasicData"].addForm.addCaliberInput.forEach(
            (value, index) =>
              (param += `&caliberDefAttr${index + 1}=${value.caliberDefAttr}`)
          );
          param += `&effectDt=${dateFormat(
            this.$refs["addBasicData"].addForm.increaseEffectDt
          )}&invalidDt=${
            this.$refs["addBasicData"].addForm.increaseInvalidDt
          }&memo=${
            this.$refs["addBasicData"].addForm.increaseMemo
          }&calcFormula=${
            this.$refs["addBasicData"].addForm.increaseCalFormula
          }&uuid=&idxDeep=`;
          let params = {
            cnName: this.$refs["addBasicData"].addForm.cnName,
            idxNo: this.$refs["addBasicData"].addForm.idxNo,
            useCase: this.$refs["addBasicData"].addForm.increaseUseCase,
            zblx: this.$refs["addBasicData"].addForm.increaseZblx,
            attr3: this.$refs["addBasicData"].addForm.increaseAttr3,
            calcUnitTypeCd: this.$refs["addBasicData"].addForm
              .increaseCalcUnitTypeCd,
            statMethodCd: this.$refs["addBasicData"].addForm
              .increaseStatMethodCd,
            statFreq: this.$refs["addBasicData"].addForm.increaseStatFreq,
            effectDt: dateFormat(
              this.$refs["addBasicData"].addForm.increaseEffectDt
            ),
            invalidDt: this.$refs["addBasicData"].addForm.increaseInvalidDt,
            memo: this.$refs["addBasicData"].addForm.increaseMemo,
            calcFormula: this.$refs["addBasicData"].addForm.increaseCalFormula,
            uuid: "",
            idxDeep: "",
            idxAttr: this.$refs["editBasicData"].editForm.increaseIdxAttr.join(
              ","
            ),
          };
          this.$refs["addBasicData"].addForm.addCaliberInput.forEach(
            (value, index) => {
              params["caliberNo" + (index + 1)] = value.caliberNo;
            }
          );
          this.$refs["addBasicData"].addForm.addCaliberInput.forEach(
            (value, index) => {
              params["caliberDefAttr" + (index + 1)] = value.caliberDefAttr;
            }
          );
          DatasCenter.checkUniqueIndexBase(params)
            .then((response) => this.handleCheckUnique(response))
            .catch(function (error) {
              console.log(error);
            });
        }
      });
    },
    //新增-处理checkUnique结果,调用保存接口
    handleCheckUnique(response) {
      if (response.data == true) {
        var param = `cnName=${this.$refs["addBasicData"].addForm.cnName}&idxNo=${this.$refs["addBasicData"].addForm.idxNo}&useCase=${this.$refs["addBasicData"].addForm.increaseUseCase}&zblx=${this.$refs["addBasicData"].addForm.increaseZblx}&attr3=${this.$refs["addBasicData"].addForm.increaseAttr3}&calcUnitTypeCd=${this.$refs["addBasicData"].addForm.increaseCalcUnitTypeCd}&statMethodCd=${this.$refs["addBasicData"].addForm.increaseStatMethodCd}&statFreq=${this.$refs["addBasicData"].addForm.increaseStatFreq}`;
        this.$refs["addBasicData"].addForm.increaseIdxAttr.forEach(
          (value) => (param += `&idxAttr=${value}`)
        );
        this.$refs["addBasicData"].addForm.addCaliberInput.forEach(
          (value, index) =>
            (param += `&caliberNo${index + 1}=${value.caliberNo}`)
        );
        this.$refs["addBasicData"].addForm.addCaliberInput.forEach(
          (value, index) =>
            (param += `&caliberDefAttr${index + 1}=${value.caliberDefAttr}`)
        );

        param += `&effectDt=${dateFormat(
          this.$refs["addBasicData"].addForm.increaseEffectDt
        )}&invalidDt=${dateFormat(
          this.$refs["addBasicData"].addForm.increaseInvalidDt
        )}&memo=${
          this.$refs["addBasicData"].addForm.increaseMemo
        }&calcFormula=${
          this.$refs["addBasicData"].addForm.increaseCalFormula
        }&uuid=&idxDeep=`;
        let caliberNo = {};
        this.$refs["addBasicData"].addForm.addCaliberInput.forEach(
          (value, index) => {
            caliberNo["caliberNo" + (index + 1)] = value.caliberNo;
          }
        );
        if (this.$refs["addBasicData"].addForm.increaseInvalidDt) {
          this.$refs["addBasicData"].addForm.increaseInvalidDt = dateFormat(
            this.$refs["addBasicData"].addForm.increaseInvalidDt
          );
        }

        let params = {
          cnName: this.$refs["addBasicData"].addForm.cnName,
          idxNo: this.$refs["addBasicData"].addForm.idxNo,
          useCase: this.$refs["addBasicData"].addForm.increaseUseCase,
          zblx: this.$refs["addBasicData"].addForm.increaseZblx,
          attr3: this.$refs["addBasicData"].addForm.increaseAttr3,
          calcUnitTypeCd: this.$refs["addBasicData"].addForm
            .increaseCalcUnitTypeCd,
          statMethodCd: this.$refs["addBasicData"].addForm.increaseStatMethodCd,
          statFreq: this.$refs["addBasicData"].addForm.increaseStatFreq,
          effectDt: dateFormat(
            this.$refs["addBasicData"].addForm.increaseEffectDt
          ),
          invalidDt: this.$refs["addBasicData"].addForm.increaseInvalidDt,
          memo: this.$refs["addBasicData"].addForm.increaseMemo,
          calcFormula: this.$refs["addBasicData"].addForm.increaseCalFormula,
          uuid: "",
          attr:
            this.$refs["addBasicData"].addForm.increaseAttr2 +
            "," +
            this.$refs["addBasicData"].addForm.increaseAttr3,
          idxAttr: this.$refs["addBasicData"].addForm.increaseIdxAttr.join(","),
          //放在url路径上
          baseIndexCal: caliberNo,
        };
        this.$refs["addBasicData"].addForm.addCaliberInput.forEach(
          (value, index) => {
            params["caliberDefAttr" + (index + 1)] = value.caliberDefAttr;
          }
        );
        DatasCenter.saveIndexBase(params)
          .then((response) => this.handleSave(response))
          .catch(function (error) {
            console.log(error);
          });
        return;
      }
      if (response.data == false) {
        this.$message({
          message: "当前指标编号或数据已存在，请重新输入",
          type: "error",
        });
      }
      if (response.code != 200) {
        this.$message({
          message: response.message,
          type: "error",
        });
      }
    },
    //新增-处理save结果
    handleSave(response) {
      if (response.code == 200) {
        this.$message({
          message: "数据保存成功",
          type: "success",
        });
        this.addBasicVisible = false;
        this.searchFunc();
      } else {
        this.$message({
          showClose: true,
          message: response.message,
          type: "error",
        });
      }
    },
    // 点击新增弹框确定按钮
    // 点击新增弹框取消按钮

    // 点击编辑弹框 传递uuid获取toform接口
    editBasic(item) {
      let selection = this.$refs.multipleTable.selection;
      console.log(selection, "选择的数据");
      if (selection.length != 1) {
        return this.$message.warning("请选择一条数据进行修改");
      }
      this.id = selection[0].uuid;
      this.$refs["editBasicData"].uuid = selection[0].uuid;
      //   this.id = item.uuid;

      (this.editBasicVisible = true),
        (this.$refs["editBasicData"].caliberTableDataShow = false);
      this.$refs["editBasicData"].caliberTableIconShow = [];
      // 获取回显信息
      this.$refs["editBasicData"].toForm();
    },
    //编辑编辑编辑编辑
    editBasicM() {
      if (this.$refs["editBasicData"].editForm.increaseInvalidDt) {
        this.$refs["editBasicData"].editForm.increaseInvalidDt = dateFormat(
          this.$refs["editBasicData"].editForm.increaseInvalidDt
        );
      }
      if (this.$refs["editBasicData"].editForm.increaseEffectDt) {
        this.$refs["editBasicData"].editForm.increaseEffectDt = dateFormat(
          this.$refs["editBasicData"].editForm.increaseEffectDt
        );
      }
      this.$refs["editBasicData"].$refs.editForm.validate((valid) => {
        if (valid) {
          var param = `cnName=${this.$refs["editBasicData"].editForm.cnName}&idxNo=${this.$refs["editBasicData"].editForm.idxNo}&useCase=${this.$refs["editBasicData"].editForm.increaseUseCase}&zblx=${this.$refs["editBasicData"].editForm.increaseZblx}&attr3=${this.$refs["editBasicData"].editForm.increaseAttr3}&calcUnitTypeCd=${this.$refs["editBasicData"].editForm.increaseCalcUnitTypeCd}&statMethodCd=${this.$refs["editBasicData"].editForm.increaseStatMethodCd}&statFreq=${this.$refs["editBasicData"].editForm.increaseStatFreq}`;
          this.$refs["editBasicData"].editForm.increaseIdxAttr.forEach(
            (value) => (param += `&idxAttr=${value}`)
          );
          this.$refs["editBasicData"].editForm.addCaliberInput.forEach(
            (value, index) =>
              (param += `&caliberNo${index + 1}=${value.caliberNo}`)
          );
          this.$refs["editBasicData"].editForm.addCaliberInput.forEach(
            (value, index) =>
              (param += `&caliberDefAttr${index + 1}=${value.caliberDefAttr}`)
          );
          param += `&effectDt=${this.$refs["editBasicData"].editForm.increaseEffectDt}&invalidDt=${this.$refs["editBasicData"].editForm.increaseInvalidDt}&memo=${this.$refs["editBasicData"].editForm.increaseMemo}&calcFormula=${this.$refs["editBasicData"].editForm.increaseCalFormula}&uuid=${this.id}&idxDeep=`;
          let params = {
            cnName: this.$refs["editBasicData"].editForm.cnName,
            idxNo: this.$refs["editBasicData"].editForm.idxNo,
            useCase: this.$refs["editBasicData"].editForm.increaseUseCase,
            zblx: this.$refs["editBasicData"].editForm.increaseZblx,
            attr3: this.$refs["editBasicData"].editForm.increaseAttr3,
            calcUnitTypeCd: this.$refs["editBasicData"].editForm
              .increaseCalcUnitTypeCd,
            statMethodCd: this.$refs["editBasicData"].editForm
              .increaseStatMethodCd,
            statFreq: this.$refs["editBasicData"].editForm.increaseStatFreq,
            effectDt: dateFormat(
              this.$refs["editBasicData"].editForm.increaseEffectDt
            ),
            invalidDt: this.$refs["editBasicData"].editForm.increaseInvalidDt,
            memo: this.$refs["editBasicData"].editForm.increaseMemo,
            calcFormula: this.$refs["editBasicData"].editForm
              .increaseCalFormula,
            uuid: this.id,
            idxDeep: "",
            idxAttr: this.$refs["editBasicData"].editForm.increaseIdxAttr.join(
              ","
            ),
          };
          // this.$refs["editBasicData"].editForm.increaseIdxAttr.forEach(
          //   (value) => (params.idxAttr = value)
          // );
          this.$refs["editBasicData"].editForm.addCaliberInput.forEach(
            (value, index) => {
              params["caliberNo" + (index + 1)] = value.caliberNo;
            }
          );
          this.$refs["editBasicData"].editForm.addCaliberInput.forEach(
            (value, index) => {
              params["caliberDefAttr" + (index + 1)] = value.caliberDefAttr;
            }
          );
          DatasCenter.checkUniqueIndexBase(params)
            .then((response) => this.handleCheckUniqueEdit(response))
            .catch(function (error) {
              console.log(error);
            });
        }
      });
    },
    //编辑-处理checkUnique结果,调用保存接口
    handleCheckUniqueEdit(response) {
      if (response.data == true) {
        let caliberNo = {};
        if (this.$refs["editBasicData"].editForm.increaseInvalidDt) {
          this.$refs["editBasicData"].editForm.increaseInvalidDt = dateFormat(
            this.$refs["editBasicData"].editForm.increaseInvalidDt
          );
        }
        let params = {
          cnName: this.$refs["editBasicData"].editForm.cnName,
          idxNo: this.$refs["editBasicData"].editForm.idxNo,
          useCase: this.$refs["editBasicData"].editForm.increaseUseCase,
          zblx: this.$refs["editBasicData"].editForm.increaseZblx,
          attr3: this.$refs["editBasicData"].editForm.increaseAttr3,
          calcUnitTypeCd: this.$refs["editBasicData"].editForm
            .increaseCalcUnitTypeCd,
          statMethodCd: this.$refs["editBasicData"].editForm
            .increaseStatMethodCd,
          statFreq: this.$refs["editBasicData"].editForm.increaseStatFreq,
          effectDt: dateFormat(
            this.$refs["editBasicData"].editForm.increaseEffectDt
          ),
          invalidDt: this.$refs["editBasicData"].editForm.increaseInvalidDt,
          memo: this.$refs["editBasicData"].editForm.increaseMemo,
          calcFormula: this.$refs["editBasicData"].editForm.increaseCalFormula,
          uuid: this.id,
          baseIndexCal: caliberNo,
          attr:
            this.$refs["editBasicData"].editForm.increaseAttr2 +
            "," +
            this.$refs["editBasicData"].editForm.increaseAttr3,
          idxAttr: this.$refs["editBasicData"].editForm.increaseIdxAttr.join(
            ","
          ),
        };
        this.$refs["editBasicData"].editForm.addCaliberInput.forEach(
          (value, index) => {
            caliberNo["caliberNo" + (index + 1)] = value.caliberNo;
            params["caliberDefAttr" + (index + 1)] = value.caliberDefAttr;
          }
        );
        DatasCenter.saveIndexBase(params)
          .then((response) => this.handleEditSave(response))
          .catch(function (error) {
            console.log(error);
          });
        return;
      }
      if (response.data == false) {
        this.$message({
          message: "当前指标编号或数据已存在，请重新输入",
          type: "error",
        });
      }
    },
    //编辑-处理save结果
    handleEditSave(response) {
      if (response.code == 200) {
        this.$message({
          message: "数据修改成功",
          type: "success",
        });
        this.editBasicVisible = false;
        this.searchFunc();
      } else {
        this.$message({
          message: response.message,
          type: "error",
        });
      }
    },
    //编辑编辑编辑编辑
    //
    // 增加关闭弹框
    closeAddDialog() {
      this.addBasicVisible = false;
    },
    // 编辑关闭弹框
    closeEditDialog() {
      this.editBasicVisible = false;
    },
    // 取消
    cancelBasicM() {
      this.addBasicVisible = false;
    },
    cancelEditBasicM() {
      this.editBasicVisible = false;
      this.$refs["editBasicData"].editForm.increaseIdxAttr = [];
    },
    // 展开
    expendSearch() {
      this.shrink = false;
    }, // 收起
    shrinkSearch() {
      this.shrink = true;
    },
    // 处理页面sizes
    handlePaginationSizes(value) {
      this.size = Number(value);
      this.getPageBasicTableList();
    },

    //处理当前页
    handlePaginationCurrent(value) {
      this.page = Number(value);
      this.getPageBasicTableList();
      window.removeEventListener("keydown", function () {});
    },

    // 处理刷新回调 回调参数：boolean
    handlePaginationRefresh() {
      this.getPageBasicTableList();
    },

    // 重置
    reset() {
      (this.searchForm.effectDt = new Date()),
        (this.searchForm.idxNo = ""),
        (this.searchForm.cnName = ""),
        (this.searchForm.attr3 = ""),
        (this.searchForm.statMethodCd = ""),
        (this.searchForm.zblx = ""),
        (this.searchForm.useCase = "");
    },
    //
    // 删除
    // 删除框取消
    cancel() {
      this.centerDialogVisible = false;
    },
    // 删除框确定
    sure() {
      let form = {
        id: this.id,
      };
      DatasCenter.removeIndexBase(form).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: "数据删除成功",
            type: "success",
          });
          this.searchFunc();
        } else {
          this.$message({
            message: res.message,
            type: "error",
          });
        }
      });
      this.centerDialogVisible = false;
    },
    // 删除
    deleteFunc() {
      let selection = this.$refs.multipleTable.selection;
      if (selection.length < 1) {
        return this.$message.warning("请选择要删除的数据");
      }
      this.$confirm("你确认要删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = {
            ids: selection.map((v) => v.uuid),
          };
          DatasCenter.removeIndexBase(params)
            .then((res) => {
              if (res.code == 200) {
                this.searchFunc();
              } else {
                this.$message.error(res.message);
              }
            })
            .catch((err) => {});
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style lang="scss" scoped>
</style>